/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef QHKP_H
#define QHKP_H

//chnages list (latest above)
//added NOPIN define
#include <iostream>
#include <QFile>
#include <QDataStream>

#include <QtDebug>
/**
A Qt wraper class for the HackerPort driver

	@author Archimedes Projekt <thelsb@gmail.com>
*/

//macro for number of elements in an array
#define arrElements( array ) (sizeof( (array) ) / sizeof( (array)[0] ))
#define NOPIN 0
#define PIN0 0x01
#define PIN1 0x02
#define PIN2 0x04
#define PIN3 0x08
#define PIN4 0x10
#define PIN5 0x20
#define PIN6 0x40
#define PIN7 0x80

struct t_hkp_drv_own;

class QHkp : public QFile
{
Q_OBJECT
public:
  enum port{A='A',B, analogA, analogB, analogC, analogD, analogE, analogAll };
  //enum pin {PIN0=0, PIN1, PIN2, PIN3, PIN4, PIN5, PIN6, PIN7};
public:
    QHkp(QString hkpDev="/dev/hkp0",QObject *parent = 0);
    ~QHkp();
    QString& device();
    void setHkpDevice(const QString &dev);
    QHkp& operator=(const QHkp& instance);
    int ownInputPort(port p);
    int ownOutputPort(port p);
    bool isPortOwned(port p,bool inPort);
    void releasePort(port p,bool inPort);
    void setOn(port p,int pinMask) ;
    void setOff(port p, int pinMask);
    void ownPort(port p,bool inPort);
    quint8 getPins(port p);
    void clearOutPort(port p);

protected:
    QString m_dev; //device name
    int m_hkp_fd; //hkp device descriptor
    bool m_open;
    quint8 m_outAdata,  //holds the pin status of the out port
           m_outBdata;
    private:
        struct t_hkp_drv_own *m_ownedPorts;
                             
    
public slots:
    int writeData(port p, unsigned char data);
    int readData(port p,unsigned char *data);
    bool pin(port p,bool inPort,int pin);
    void initPorts();
    
signals:
    void dataReady(unsigned char);    

};


typedef enum CmdType { READ_IN,READ_OUT,WRITE}; //READ_IN - reads in port pins, READ_OUT - reads out port pins
typedef enum CmdMode { BYTE,PIN,PIN_ON, PIN_OFF }; //BYTE and PIN are reading modes, PIN_ON and PIN_OFF are writting modes
typedef struct {
    CmdType type;
    CmdMode mode;
    int pin;
    QHkp::port port;
    quint8 data;
    quint8 pad[500];
}HkpCommand;

inline QDataStream & operator<<( QDataStream &s,const HkpCommand &hkpCoommand)
{
    if(s.writeRawData((char*)&hkpCoommand,sizeof(hkpCoommand))==-1)
    return s;
}

inline QDataStream & operator>>( QDataStream &s, const HkpCommand &hkpCoommand)
{
    if(s.readRawData((char*)&hkpCoommand,sizeof(hkpCoommand))==-1)
        qDebug()<<"reading hkp"; 
    return s;
}

inline QDataStream & operator<<( QDataStream &s,const CmdType &cmdType)
{
    if(s.writeRawData((char*)&cmdType,sizeof(cmdType))==-1)
    return s;
}

inline QDataStream & operator>>( QDataStream &s, const CmdType &cmdType)
{
    if(s.readRawData((char*)&cmdType,sizeof(cmdType))==-1)
        qDebug()<<"reading hkp"; 
    return s;
}

inline QDataStream & operator<<( QDataStream &s,const QHkp::port &port)
{
    if(s.writeRawData((char*)&port,sizeof(port))==-1)
        qDebug()<<"hkp setting port"; 
    return s;
}

inline QDataStream & operator>>( QDataStream &s, const QHkp::port &port)
{
    if(s.readRawData((char*)&port,sizeof(port))==-1)
        std::cout<<"hkp setting port"; 
    return s;
}

#endif


